package com.yonyou.pmclouds.measureindex.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.EnableStatusConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.measureindex.entity.*;
import com.yonyou.pmclouds.measureindex.service.rmiitf.MeasureIndexQuery;
import com.yonyou.pmclouds.measureindex.service.rmiitf.MeasureIndexService;
import com.yonyou.pmclouds.organization.entity.OrganizationShowVO;
import com.yonyou.pmclouds.outter.tenant.TenantInfoQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.tenant.entity.TenantInfo;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value={"/measureIndex","/mobile/measureIndex"})
public class MeasureIndexController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private MeasureIndexService measureIndexService;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private MeasureIndexQuery measureIndexQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectSimpleQuery query;
    private final String MEASUREINDEXVOS_KEY = "measureIndexVOS";
    @Autowired
    private TenantInfoQuery tenantInfoQuery;

    @RequestMapping("/list")
    public PageObject<MeasureIndexVO> getList(String timestamp, String keyword, Integer pageStart, Integer pageSize, Integer enableStatus) throws BusinessException {

        //由于前后端时间可能不一致导致数据不能立马出现暂时时间戳先取后台
        timestamp = String.valueOf(new Date().getTime());

        timestamp = timestamp.substring(0, 10);

        if (enableStatus == null) {
            enableStatus = new Integer(EnableStatusConst.ENABLE);
        }
        if (pageSize == null || pageSize == 0) {
            pageStart = 0;
            pageSize = 1000;
        }

        return measureIndexService.queryMeasureIndexByPage(timestamp, keyword, pageStart, pageSize, enableStatus);
    }

    @RequestMapping("/add")
    public MeasureIndexVO add(@RequestBody MeasureIndexVO measureIndexVO) throws BusinessException {

        return measureIndexService.add(measureIndexVO);
    }

    @RequestMapping("/modify")
    public MeasureIndexVO modifyStatus(@RequestBody MeasureIndexVO measureIndexVO) throws BusinessException {

        return measureIndexService.modifyStatus(measureIndexVO);
    }

    @RequestMapping("/delete")
    public String delete(@RequestBody MeasureIndexVO measureIndexVO) throws BusinessException {

        return measureIndexService.delete(measureIndexVO);
    }

    @RequestMapping("/save")
    public Map<String,Object> save(@RequestBody MeasureIndexDTO data) throws BusinessException {

        MeasureIndexVO[] measureIndexVOS = data.getMeasureIndexVOS();

        return measureIndexService.saveAddAndUpdate(measureIndexVOS);
    }

    /**
     * 指标档案参照查询
     *
     * @param pkTenant
     * @param keyword
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/refQuery")
    public List<MeasureIndexRefVO> refQuery(@RequestParam(value = "pkTenant", required = false) String pkTenant,
                                            @RequestParam(value = "keyword", required = false) String keyword) throws BusinessException {
        if (StringUtils.isEmpty(pkTenant))
            pkTenant = RuntimeEnvironment.getTenantId();

        List<MeasureIndexRefVO> result = measureIndexQuery.indexRefQuery(pkTenant, keyword);
        return result;
    }
    /**
     * 该项目下的指标
     */
    @RequestMapping(value="/queryByProjectIdAndOrg")
    public List<MeasureIndexVO> queryByProjectIdAndOrg(@RequestParam(value = "projectId",required = false) String projectId,@RequestParam(value = "pkOrg",required = false)String pkOrg) throws BusinessException {
        List<MeasureIndexVO> indexVOList=new ArrayList<>();
        if (projectId==null){
            if (pkOrg==null){
                return new ArrayList<>();
            }
            String tenantId = RuntimeEnvironment.getTenantId();
            List<String> projectIds=new ArrayList<>();
            ProjectVO[] projectVOS=new ProjectVO[0];
            if (pkOrg.equals(tenantId)){
                projectVOS = query.queryProjectIdByTenantOrgExt(tenantId, null);
            }else {
                projectVOS = query.queryProjectIdByTenantOrgExt(tenantId, pkOrg);
            }
            if (ArrayUtils.isEmpty(projectVOS)){
                return new ArrayList<>();
            }
            for (ProjectVO projectVO:projectVOS){
                projectIds.add(projectVO.getPkProject());
            }
            indexVOList= measureIndexQuery.queryByProjectIds(projectIds.toArray(new String[0]));
            return indexVOList;
        }else{
            indexVOList=measureIndexQuery.queryByProjectIds(new String[]{projectId});
        }
        return indexVOList;
    }
    /**
     * 单项目指标报表查询
     */
    @RequestMapping("/indexExcelQuery")
    public List<IndexExcelShowVO> indexExcelQuery(@RequestBody IndexExcelQueryVO queryVO) throws BusinessException {
        List<IndexExcelShowVO> indexExcelShowVOS = measureIndexQuery.queryIndexBySingleProject(queryVO);
        return indexExcelShowVOS;
    }
    /**
     * 组织项目指标报表查询
     */
    @RequestMapping("/indexExcelOrgQuery")
    public List<OrganizationShowVO> indexExcelOrgQuery(@RequestBody IndexExcelQueryVO queryVO) throws BusinessException {
        OrganizationShowVO organizationShowVO = measureIndexQuery.queryIndexofMultiOrg(queryVO);
        if (organizationShowVO==null||organizationShowVO.getProjectTypeShowVOS()==null||organizationShowVO.getProjectTypeShowVOS().size()==0){
            return null;
        }
        String tenantId = RuntimeEnvironment.getTenantId();
        if (queryVO.getPkOrg().equals(tenantId)){
            TenantInfo tenantInfo = tenantInfoQuery.queryTenantInfoById(tenantId);
            organizationShowVO.setSname(tenantInfo.getTenantName());
        }
        List<OrganizationShowVO> showVOS=new ArrayList<>();
        showVOS.add(organizationShowVO);
        return showVOS;
    }
}
